package com.park.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.weaver.IWeavingSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.AuthUser;
import com.park.model.Park;
import com.park.model.User;
import com.park.service.AuthorityService;
import com.park.service.FeeOperatorService;
import com.park.service.FeeoperatortouserService;
import com.park.service.ParkService;
import com.park.service.UserService;
import com.park.service.Utility;

@Controller
@RequestMapping("/feeoperatortouser")
public class FeeoperatortouserController {
	
	@Autowired
	FeeoperatortouserService feeoperatortouserService;
	@Autowired
	FeeOperatorService feeOperatorService;
	@Autowired
	UserService userService;
	@Autowired
	AuthorityService authorityService;
	@Autowired
	ParkService parkService;
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert() {
		Map<String, Object> result=new HashMap<>();
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="delete/{id}",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@PathVariable("id")int id){
		Map<String, Object> result=new HashMap<>();
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update() {
		Map<String, Object> result=new HashMap<>();
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="selectByPrimaryKey/{id}",method=RequestMethod.GET,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectByPrimaryKey(@PathVariable("id")int id){
		Map<String, Object> result=new HashMap<>();
		return Utility.gson.toJson(result);
	}
	/*
	 * 根据用户名返回POS机信息
	 * */
	@RequestMapping(value="selectPosInfoByuserIdV2",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectPosInfoByuserIdV2(@RequestBody Map<String, String> args) {
		Map<String, Object> result=new HashMap<>();
		String username=args.get("userName");
		AuthUser user=authorityService.getUserByUsername(username);
		if (user==null) {
			result.put("status", 1002);
			result.put("message", "没有此用户");
			return Utility.gson.toJson(result);
		}
		List<Map<String, Object>> data= feeoperatortouserService.selectPosInfoByuserId(user.getId());
		for(Map<String, Object> map : data){
			int parkId = (int)map.get("parkId");
			Park park = parkService.getParkById(parkId);
			map.put("parkName", park.getName());
		}
		result.put("status", 1001);
		result.put("body", data);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="selectPosInfoByuserId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectPosInfoByuserId(@RequestBody Map<String, String> args) {
		Map<String, Object> result=new HashMap<>();
		String username=args.get("userName");
		AuthUser user=authorityService.getUserByUsername(username);
		if (user==null) {
			result.put("status", 1002);
			result.put("message", "没有此用户");
			return Utility.gson.toJson(result);
		}
		List<Map<String, Object>> data= feeoperatortouserService.selectPosInfoByuserId(user.getId());
		result.put("status", 1001);
		result.put("body", data);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="selectFeeOperatorInfoByuserId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectFeeOperatorInfoByuserId(@RequestBody Map<String, String> args) {
		Map<String, Object> result=new HashMap<>();
		String username=args.get("userName");
		AuthUser user=authorityService.getUserByUsername(username);
		if (user==null) {
			result.put("status", 1002);
			result.put("message", "没有此用户");
			return Utility.gson.toJson(result);
		}
		List<Map<String, Object>> data= feeoperatortouserService.selectFeeOperatorInfoByuserId(user.getId());
		result.put("status", 1001);
		result.put("body", data);
		return Utility.gson.toJson(result);
	}
}
